home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / oobr / tree-nx / Tree.m < prev    next >
Encoding:
Text File  |  1995-03-25  |  4.2 KB  |  149 lines

  1. //
  2. //    Tree.m -- a generic class to build tree data structures
  3. //        This class requires the String class, also by Don Yacktman.
  4. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  5. //                All rights reserved.
  6. //
  7. //        You may use and copy this class freely as long as you
  8. //        comply with the following terms:
  9. //            (1) If you use this class in an application which you
  10. //                intend to sell commercially, as shareware, or otherwise,
  11. //                you may only do so with express written permission
  12. //                of the author.  Use in applications which will
  13. //                be distributed free of charge is encouraged.
  14. //            (2) You must include the source code to this object and
  15. //                all accompanying documentation with your application,
  16. //                or provide it to users if requested, free of charge.
  17. //            (3) Do not remove the author's name or any of the
  18. //                copyright notices
  19. //
  20.  
  21. #import "Tree.h"
  22.  
  23. @implementation Tree
  24.  
  25. - init            // special init for this class
  26. {
  27.     [self initLabel:""];
  28.     return self;
  29. }
  30.  
  31. - initLabel:(const char *)newLabel
  32. {
  33.     [super init];
  34.     notCollapsed = YES;
  35.     // set up a list class to hold pointers to other Tree objects (branches).
  36.     branches = [[List alloc] init];    // make a dynamic-sized array of objects
  37.     label = [[String alloc] init];    // make a string for the label
  38.     [label setString:newLabel];
  39.     return self;
  40. }
  41.  
  42. - initLabelString:string    // send in a String object
  43. {
  44.     [self init];
  45.     if (label) [label free];
  46.     label = string;
  47.     return self;
  48. }
  49.  
  50. - setLabel:(const char *)newLabel
  51. {
  52.     [label setString:newLabel];
  53.     return self;
  54. }
  55.  
  56. - setValue:newValue
  57. {
  58.     if (value) [value free];
  59.     value = newValue;
  60.     return self;
  61. }
  62.  
  63. - free            // clean up our mess
  64. {
  65.     [branches freeObjects];    // free the branches we're responsible for
  66.     // (I assumed that each branch only has one parent...if that's not
  67.     // the case, then the above line should be axed.)
  68.     [branches free];        // get rid of the List object
  69.     return [super free];
  70. }
  71.  
  72. - addBranch:child    // add a new child node
  73. {    // add to the end of the "branches" List object
  74.     return [branches addObject:child];
  75. }
  76.  
  77. - dumpTree:(NXStream *)file level:(int)lev indent:(const char *)ind
  78. {
  79.     int i; BOOL doKids;
  80.     
  81.     for (i=0; i<lev; i++) NXPrintf(file, "%s", ind);    // indent
  82.     NXPrintf(file, "%s", [label stringValue]);        // and print label
  83.     doKids = [self moreData:file level:lev indent:ind];
  84.     NXPrintf(file, "\n");                           // print newline
  85.     if (doKids)
  86.         for (i=0; i<[branches count]; i++)        // then do children
  87.             [[branches objectAt:i]
  88.              dumpTree:file level:(lev + 1) indent:ind];
  89.     return self;
  90. }
  91.  
  92. - (BOOL)moreData:(NXStream *)file level:(int)lev indent:(const char *)ind
  93. {    // Subclass responsibility -- you can dynamically control collapsing
  94.     // (for example, cut off at a certain level, etc.) and also add info
  95.     // to the end of a dumped node's line from here.  Be sure to message
  96.     // super when you override this method; if this method returns a NO
  97.     // then you should return a NO, regardless.  Don't just use the
  98.     // notCollapsed instance var, since it may change in the future; look
  99.     // at the return value from super!
  100.     //
  101.     // Here's how you might override to keep from printing levels deeper
  102.     // than level 2 (remember that the root level is zero):
  103.     //
  104.     // - (BOOL)moreData:(NXStream *)file level:(int)lev indent:(const char *)ind
  105.     // {
  106.     //    if ((lev > 2) || ![super moreData:file level:lev indent:ind])
  107.     //     return NO;
  108.     //    return YES;
  109.     // }
  110.     //
  111.     return notCollapsed;
  112. }
  113.  
  114. - (int)width
  115. {
  116.     int num = [branches count];
  117.     int i, count = 0;
  118.     
  119.     if (!num) return 1;    // No children, so we're only one node wide...
  120.     
  121.     // have kids, so sum up their widths.
  122.     for (i=0; i<num; i++) count += [[branches objectAt:i] width];
  123.     return count;
  124. }
  125.  
  126. - (int)depth
  127. {
  128.     int num = [branches count];
  129.     int i, temp, deepest = 1;
  130.     
  131.     if (!num) return 1;    // No children, so only one node deep
  132.     
  133.     // have kids, so see which branch is deepest.
  134.     for (i=0; i<num; i++) {
  135.         temp = [[branches objectAt:i] depth];
  136.         if (temp > deepest) deepest = temp;
  137.     }
  138.     return (deepest + 1);    // we are one deeper than the deepest child.
  139. }
  140.  
  141. - branches { return branches; }
  142. - collapse { notCollapsed = NO; return self; }
  143. - uncollapse { notCollapsed = YES; return self; }
  144. - (BOOL)collapsed { return !notCollapsed; }
  145. - (const char *)label { return [label stringValue]; }
  146. - (const char *)value { return [value stringValue]; }
  147.  
  148. @end
  149.